//
//  JMScrollView.m
//  NewiOSProject
//
//  Created by li on 2023/9/4.
//  Copyright © 2023 范智渊. All rights reserved.
//

#import "LayoutScrollView.h"

@implementation LayoutScrollView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview: self.rootStack];
        
        self.leftMargin = [self.rootStack.leftAnchor constraintEqualToAnchor:self.contentLayoutGuide.leftAnchor constant:0];
        self.rightMargin = [self.rootStack.rightAnchor constraintEqualToAnchor:self.contentLayoutGuide.rightAnchor constant:0];
        self.topMargin = [self.rootStack.topAnchor constraintEqualToAnchor:self.contentLayoutGuide.topAnchor constant:0];
        self.bottomMargin = [self.rootStack.bottomAnchor constraintEqualToAnchor:self.contentLayoutGuide.bottomAnchor constant:0];
        
        [self.rootStack.heightAnchor constraintGreaterThanOrEqualToAnchor:self.frameLayoutGuide.heightAnchor].active = YES;
        [self.rootStack.widthAnchor constraintGreaterThanOrEqualToAnchor:self.frameLayoutGuide.widthAnchor].active = YES;
        
        self.leftMargin.active = YES;
        self.rightMargin.active = YES;
        self.topMargin.active = YES;
        self.bottomMargin.active = YES;
    }
    return self;
}

-(void)scrollToItemCenter:(NSInteger)index position:(LayoutScrollViewPosition)position offset:(CGPoint)offset animation:(BOOL)animation {
    
    if (animation) {
        [UIView animateWithDuration:0.25 animations:^{
            [self scrollTo:index position: position offset:offset];
        }];
    }else {
        [self scrollTo:index position: position offset:offset];
    }
}

-(void)scrollTo:(NSInteger)index position: (LayoutScrollViewPosition)positon offset: (CGPoint)offset {
    
    if (index < self.rootStack.contentView.arrangedSubviews.count) {
        UIView *view = self.rootStack.contentView.arrangedSubviews[index];
        CGRect rect = [self.rootStack.contentView convertRect:view.frame toView:self];
        
        CGFloat x = CGRectGetMidX(rect) + offset.x;
        CGFloat y = CGRectGetMidY(rect) + offset.y;
        
        if ((x - self.frame.size.width / 2.0) <= 0) {
            x = 0;
        }else if ((x + self.frame.size.width / 2.0) >= self.contentSize.width) {
            x = self.contentSize.width - self.frame.size.width;
        }else {
            x = x - self.frame.size.width / 2.0;
        }
        
        if ((y - self.frame.size.height / 2.0) <= 0) {
            y = 0;
        }else if ((y + self.frame.size.height / 2.0) >= self.contentSize.height) {
            y = self.contentSize.height - self.frame.size.height;
        }else {
            
            self.contentOffset = CGPointMake(x, y);
        }
    }
}

- (LayoutView *)rootStack {
    if (!_rootStack) {
        self.rootStack = [LayoutView new];
        _rootStack.translatesAutoresizingMaskIntoConstraints = NO;

    }
    return _rootStack;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
